<template>
  <div class="login-container">
    <div class="background">
      <!-- 背景图片 -->
    </div>
    <div class="header-text">
      <span>南京市古树名木</span>
      <br />
      <span>数字化智慧管理平台</span>
    </div>
    <el-card class="login-card">
      <div slot="header" class="login-header">
        <span>账号登录</span>
      </div>
      <el-form :model="form" ref="form" label-width="0" class="login-form">
        <el-form-item>
          <el-input v-model="form.username" placeholder="请输入用户名/手机号" />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="form.rememberMe">记住密码</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
        <div class="login-footer">
          <span>用户注册</span> | <span>忘记密码?</span>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";
import { ElForm, ElFormItem, ElInput, ElButton, ElCard, ElCheckbox } from "element-plus";
import { useRouter } from "vue-router";

export default {
  name: "Login",
  components: {
    ElForm,
    ElFormItem,
    ElInput,
    ElButton,
    ElCard,
    ElCheckbox,
  },
  data() {
    return {
      form: {
        username: "",
        password: "",
        rememberMe: false,
      },
    };
  },
  methods: {
    async handleLogin() {
      try {
        const response = await axios.post("http://localhost:4000/login", {
          username: this.form.username,
          password: this.form.password,
        });

        if (response.data === "登录成功") {
          this.$router.push("/home"); // 使用 this.$router 进行路由跳转
        } else {
          alert("用户名或密码错误");
        }
      } catch (error) {
        console.error("登录请求失败:", error);
        alert("登录请求失败，请稍后再试");
      }
    },
  },
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url("/images/background.png");
  background-size: cover;
  background-position: center;
  z-index: -1;
}

.header-text {
  position: absolute;
  top: 30px;
  left: 50px;
  color: #ffffff;
  font-size: 33px;
  font-weight: bold;
  line-height: 1.5;
  z-index: 1;
}

.login-card {
  width: 400px;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  /* 增加阴影效果 */
}

.login-header {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}

.login-form {
  width: 100%;
}

.login-footer {
  text-align: center;
  margin-top: 10px;
  font-size: 12px;
}
</style>